package redis.client.nedis.util;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.CharsetUtil;

public class CommandUtil {

    /**
     * 将Redis命令转化为RESP协议
     *
     * @param command Redis命令
     * @return
     */
    public static ByteBuf convertToRESP(String command) {
        StringBuilder sb = new StringBuilder();

        String[] arr = command.split(" ");
        sb.append('*');
        sb.append(arr.length);
        sb.append("\r\n");
        for (String s : arr) {
            sb.append('$');
            sb.append(s.length());
            sb.append("\r\n");
            sb.append(s);
            sb.append("\r\n");
        }

        return Unpooled.copiedBuffer(sb.toString(), CharsetUtil.UTF_8);
    }

}
